<?php
namespace MkUser\Controller;

use Think\Controller;
use Think\Model;

class IndexController extends Controller
{
    public function index()
    {
    }

    public function initUser()
    {
        return;

        $user    = M("User");
        $dis_ary = array();

        $data_recd["user_name"]   = "admin";
        $data_recd["dis_name"]    = "系统管理员";
        $data_recd["password"]    = getPassWordStr("123");
        $data_recd["manage_auth"] = 1;
        array_push($dis_ary, $user->add($data_recd));
        $data_recd["user_name"]   = "vuser";
        $data_recd["dis_name"]    = "观察用户";
        $data_recd["password"]    = getPassWordStr("123");
        $data_recd["manage_auth"] = 0;
        array_push($dis_ary, $user->add($data_recd));

        for ($i = 0; $i < 150; $i++)
        {
            break;
            $data_recd["user_name"]   = "user_" . strval($i);
            $data_recd["dis_name"]    = "测试用户_user_" . strval($i);
            $data_recd["password"]    = getPassWordStr("123" . strval($i));
            $data_recd["manage_auth"] = 0;
            array_push($dis_ary, $user->add($data_recd));
        }
        // dump($dis_array);
        $this->ajaxReturn($dis_ary);
    }

    public function listUser()
    {
        $user = M("User");

        $user_list_ary       = $user->field("password", true)->page(strval(I("post.page", 1)) . "," . strval(I("post.rows", 100)))->select();
        $result_ary["total"] = $user->count();
        $result_ary["rows"]  = $user_list_ary;

        foreach ($result_ary["rows"] as $key => $val)
        {
            if ($result_ary["rows"][$key]["manage_auth"] == 1)
                $dis_val = "√";
            else
                $dis_val = "×";
            $result_ary["rows"][$key]["manage_auth_dis"] = $dis_val;
        }
        $this->ajaxReturn($result_ary);
    }

    public function getUser()
    {
    }

    public function addUser()
    {
        //如果密码为空则填入缺省密码
        if (strlen(trim(I("post.password"))) == 0)
            $_POST["password"] = "123";

        $rules = array(
            array("user_name", "require", "该项必须输入"),
            array("dis_name", "require", "该项必须输入"),

            array("user_name", "", "用户名已经存在！", Model::EXISTS_VALIDATE, "unique", Model::MODEL_INSERT)
        );

        $data = array_merge(I("post."));
        unset($data["id"]);

        $User = D("User");
        if (!$User->validate($rules)->create($data))
        {
            $result_ary = array("error" => 1, "infoAry" => $User->getError());
            $this->ajaxReturn($result_ary);
        }
        else
        {
            $User->password                = getPassWordStr(I("post.password"));
            $result_ary                    = array_merge($_POST);
            $result_ary["id"]              = $User->field("id", true)->add();
            $result_ary["manage_auth_dis"] = ($result_ary["manage_auth"] == 1) ? "√" : "×";

            $result_ary = array("error" => 0, "infoAry" => $result_ary);
            $this->ajaxReturn($result_ary);
        }
    }

    public function updateUser()
    {
        $rules = array(
            array("user_name", "require", "该项必须输入"),
            array("dis_name", "require", "该项必须输入"),

            array("user_name", "", "用户名已经存在！", Model::EXISTS_VALIDATE, "unique", Model::MODEL_UPDATE)
        );

        $data = array_merge(I("post."));

        $User = D("User");
        if (!$User->validate($rules)->create($data))
        {
            $result_ary = array("error" => 1, "infoAry" => $User->getError());
            $this->ajaxReturn($result_ary);
        }
        else
        {
            if (strlen(trim(I("post.password"))) > 0)
            {
                $User->password = getPassWordStr(I("post.password"));
                $User->save();
            }
            else
                $User->field("password", true)->save();

            $result_ary                    = array_merge(I("post."));
            $result_ary["manage_auth_dis"] = ($result_ary["manage_auth"] == 1) ? "√" : "×";

            $result_ary = array("error" => 0, "infoAry" => $result_ary);
            $this->ajaxReturn($result_ary);
        }
    }

    public function delUser()
    {
        //不能删除内置用户
        if (I("post.id") <= 2)
            return;

        $User      = M("User");
        $map["id"] = I("post.id");
        $User->where($map)->delete();
    }

    public function manageUser()
    {
        $this->assign("user", session("user"));
        $this->display();
    }
}